Editor.Init = {
  state: function () {
    Editor.STATE.CANVAS = Raphael("canvas", Editor.CONFIG.CANVAS_WIDTH, Editor.CONFIG.CANVAS_HEIGHT);
    Editor.STATE.DRAGGERCANVAS = new Editor.DraggerCanvas();
    Editor.STATE.DRAGGERCANVAS.draw();
    Editor.STATE.CMDSTACK = new JS.Command.Stack();
    Editor.STATE.DRAGGER = undefined;
    Editor.STATE.ADDSHAPE = undefined;
    Editor.STATE.LANG = Editor.LANG.EN;
    Editor.STATE.SHAPES = new JS.Hash();
    Editor.STATE.SELECTED = new Array();
  },
  lang: function () {
    document.title = Editor.STATE.LANG.TITLE;
    $("#logo").text(Editor.STATE.LANG.TITLE); 
    $("#buttonUndo").text(Editor.STATE.LANG.UNDO); 
    $("#buttonRedo").text(Editor.STATE.LANG.REDO); 
    $("#buttonAddResource").text(Editor.STATE.LANG.ADD_RESOURCE); 
    $("#buttonAddPMLJ").text(Editor.STATE.LANG.ADD_PMLJ); 
    $("#buttonAddPMLT").text(Editor.STATE.LANG.ADD_PMLT); 
    $("#buttonValidate").text(Editor.STATE.LANG.ADD_VALID); 
    $("#buttonAddLiteral").text(Editor.STATE.LANG.ADD_LITERAL); 
    $("#buttonDeleteSelected").text(Editor.STATE.LANG.DELETE_SELECTED); 
    $("#buttonToggleLangUi").text(Editor.STATE.LANG.TOGGLE_LANG_UI); 
    $("#buttonRefreshPanel").text(Editor.STATE.LANG.REFRESH_PANEL); 
    $("#buttonSendAddResource").text(Editor.STATE.LANG.SEND); 
    $("#buttonSendAddPMLJ").text(Editor.STATE.LANG.SEND); 
    $("#buttonSendAddPMLT").text(Editor.STATE.LANG.SEND); 
    $("#buttonCloseAddProperty").text(Editor.STATE.LANG.CLOSE); 
    $("#buttonSendAddProperty").text(Editor.STATE.LANG.SEND); 
    $("#buttonCloseAddResource").text(Editor.STATE.LANG.CLOSE); 
    $("#buttonCloseAddPMLJ").text(Editor.STATE.LANG.CLOSE);
    $("#buttonCloseAddPMLT").text(Editor.STATE.LANG.CLOSE); 
    $("#buttonSendAddLiteral").text(Editor.STATE.LANG.SEND); 
    $("#buttonCloseAddLiteral").text(Editor.STATE.LANG.CLOSE); 
    $("#buttonSendImport").text(Editor.STATE.LANG.SEND); 
    $("#buttonCloseImport").text(Editor.STATE.LANG.CLOSE); 
    $("#buttonCloseExport").text(Editor.STATE.LANG.CLOSE); 
    $("#buttonNew").text(Editor.STATE.LANG.NEW_DIAGRAM); 
    $("#buttonExport").text(Editor.STATE.LANG.EXPORT_DIAGRAM); 
    $("#buttonImport").text(Editor.STATE.LANG.IMPORT_DIAGRAM); 
    $("#buttonOptions").text(Editor.STATE.LANG.OPTIONS); 
    $("#buttonCloseOptions").text(Editor.STATE.LANG.CLOSE); 
    $("#buttonSendOptions").text(Editor.STATE.LANG.SEND); 
    $("#buttonAbout").text(Editor.STATE.LANG.ABOUT);
    $("#titleAddResource").text(Editor.STATE.LANG.RESOURCE); 
    $("#titleAddLiteral").text(Editor.STATE.LANG.LITERAL); 
    $("#titleImport").text(Editor.STATE.LANG.IMPORT_DIAGRAM); 
    $("#titleExport").text(Editor.STATE.LANG.EXPORT_DIAGRAM); 
    $("#titleAddLiteral").text(Editor.STATE.LANG.LITERAL); 
    $("#buttonTextImportClear").text(Editor.STATE.LANG.CLEAR); 
  },
  buttons: function () {
    $('#buttonUndo').click(Editor.Util.undo);
    $('#buttonRedo').click(Editor.Util.redo);
    $('#buttonAddResource').click(Editor.Util.prepareAddResource);
    $('#buttonAddPMLJ').click(Editor.Util.prepareAddPMLJ);
    $('#buttonAddPMLT').click(Editor.Util.prepareAddPMLT);
    $('#buttonAddLiteral').click(Editor.Util.prepareAddLiteral);
    $('#buttonValidate').click(Editor.Util.validate);
    $('#buttonToggleLangUi').click(Editor.Util.toggleLang);
    $('#buttonSendAddLiteral').click(Editor.Util.updateLiteral);
    $('#buttonCloseAddLiteral').click(Editor.Util.cancelUpdateLiteral);
    $('#buttonSendAddResource').click(Editor.Util.updatePMLP);
    $('#buttonCloseAddResource').click(Editor.Util.cancelUpdateResource);
    $('#buttonSendAddPMLJ').click(Editor.Util.updatePMLJ);
    $('#buttonCloseAddPMLJ').click(Editor.Util.cancelUpdatePMLJ);
    $('#buttonSendAddPMLT').click(Editor.Util.updatePMLT);
    $('#buttonCloseAddPMLT').click(Editor.Util.cancelUpdatePMLT);    $('#buttonSendAddProperty').click(Editor.Util.updateProperty);
    $('#buttonCloseAddProperty').click(Editor.Util.cancelUpdateProperty);
    $('#buttonDeleteSelected').click(Editor.Util.removeShapes);
    $('#buttonImport').click(Editor.File.prepareImportDiagram);
    $('#buttonSendImport').click(Editor.File.importDiagramFromUri);
    $('#buttonExport').click(Editor.File.exportDiagram);
    $('#buttonCloseExport').click(Editor.File.closeExport);
    $('#buttonCloseImport').click(Editor.File.closeImport);
    $('#buttonNew').click(Editor.File.newDiagram);
    $('#buttonOptions').click(Editor.Util.prepareOptions);
    $('#buttonSendOptions').click(Editor.Util.updateOptions);
    $('#buttonCloseOptions').click(Editor.Util.closeOptions);
    $('#buttonAbout').click(Editor.Util.about);
    $('.buttonCloseAbout').click(Editor.Util.closeAbout);
    $("#buttonTextImportClear").click(Editor.File.textImportClear);
    
    $('#canvasAdd').click(Editor.Util.canvasAddClick);
  },
  ui: function () {
    Editor.Init.lang();
    Editor.Init.buttons();
    document.onmousemove = function (e) {
      e = e || window.event;
      if (Editor.Util.isDragging()) {
        Editor.STATE.DRAGGER.onmousemove(e);
      }
    }
    document.onmouseup = function () {
      if (Editor.Util.isDragging()) {
        Editor.STATE.DRAGGER.onmouseup();
      }
    }
  },
  open: function () {
    var extraspace = 100;
    var file = Editor.Util.gup('uri');
    if (file) {
      $("#dialogWait").jqm().jqmShow();
      var url = 'importer.php?uri=' + file+'&w='+Editor.CONFIG.CANVAS_WIDTH+'&h='+Editor.CONFIG.CANVAS_HEIGHT;
      var arrObjects;
      $.ajax({
             url: url,
             type: "GET",
             dataType: "json",
             cache: false,
             async: false,
             success: function(data) {
               if(data.status == "ERROR"){
                 $('#dialogWait').jqm().jqmHide();
                 alert("Can't import file");
               }else{
                 for(var i=0; i< data.length; i++){
                   if(data[i].type == "resource" ||data[i].type == "blank"){
                     //alert(data[i].name+" "+data[i].resource.shape.bbox.x+" "+data[i].resource.shape.bbox.y);
                     aux = parseInt(data[i].resource.shape.bbox.x)+(parseInt(data[i].resource.shape.bbox.width));
                     if(Editor.CONFIG.CANVAS_WIDTH < aux){
                       Editor.CONFIG.CANVAS_WIDTH = aux+extraspace;
                     }
                     aux = parseInt(data[i].resource.shape.bbox.y)+(parseInt(data[i].resource.shape.bbox.height));
                     if(Editor.CONFIG.CANVAS_HEIGHT < aux){
                       Editor.CONFIG.CANVAS_HEIGHT = aux+extraspace;
                     }
                   }
                   if(data[i].type == "literal"){
                     //alert(data[i].text+" "+data[i].shape.bbox.x+" "+data[i].shape.bbox.y);
                     aux = parseInt(data[i].shape.bbox.x)+parseInt(data[i].shape.bbox.width);
                     if(Editor.CONFIG.CANVAS_WIDTH < aux){
                       Editor.CONFIG.CANVAS_WIDTH = aux+extraspace;
                     }
                     aux = parseInt(data[i].shape.bbox.y)+(parseInt(data[i].shape.bbox.height));
                     if(Editor.CONFIG.CANVAS_HEIGHT < aux){
                       Editor.CONFIG.CANVAS_HEIGHT = aux+extraspace;
                     }
                   }
                   Editor.STATE.CANVAS.setSize(Editor.CONFIG.CANVAS_WIDTH,Editor.CONFIG.CANVAS_HEIGHT);
                   Editor.STATE.BACKGROUND0.attr({width: Editor.CONFIG.CANVAS_WIDTH, height:Editor.CONFIG.CANVAS_HEIGHT});
                   Editor.STATE.BACKGROUND.attr({width: Editor.CONFIG.CANVAS_WIDTH, height:Editor.CONFIG.CANVAS_HEIGHT});
                 }
                 
                 //  Editor.CONFIG.CANVAS_WIDTH = $('#canvasWidth').val();
                 //  Editor.CONFIG.CANVAS_HEIGHT = $('#canvasHeight').val();
                 arrObjects = data;
               }
             }
      });
      Editor.Util.drawObjects(arrObjects);
      delete arrObjects;
      $('#dialogWait').jqm().jqmHide();

    }
  },
  init: function () {
    Editor.CONFIG.CANVAS_HEIGHT = $(window).height();//-$("#canvas").position().top;
    Editor.CONFIG.CANVAS_WIDTH = $(window).width()-$("#canvas").position().left;
    
    Editor.Init.state();
    Editor.Init.ui();
    Editor.Init.open();
    //$().mousemove(function(e){
    //$('#status').html( 'w: ' + $(window).width() + 'x' + $(window).height() + ' m: ' + e.pageX +', '+ e.pageY);
    //}); 
    
  }
}
